﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace VisionAppManager.ViewModel
{
    public class VMLog : ViewModelBase
    {
        private ObservableCollection<Log> _logs = new ObservableCollection<Log>();
        public ObservableCollection<Log> Logs
        {
            get
            {
                return _logs;
            }
        }

        public VMLog()
        {
            Messenger.Default.Register<string>(this, (msg) => ReceiveMessage(msg));
        }

        private void ReceiveMessage(string msg)
        {
            try
            {
                Application.Current?.Dispatcher.Invoke((Action)delegate
                {
                    var log = new Log
                    {
                        CreatedOn = DateTime.Now.ToString("MM-dd-HH:mm:ss.fff"),
                        ShortMessage = msg
                    };
                    Logs.Insert(0, log);
                });
            }
            catch(Exception e)
            {

            }
        }

        public class Log
        {
            public int LogLevelId { get; set; }
            public LogLevel LogLevel { get; set; }
            public string ShortMessage { get; set; }
            public string FullMessage { get; set; }
            public string CreatedOn { get; set; }
        }

        public enum LogLevel
        {
            Debug = 10,
            Information = 20,
            Warning = 30,
            Error = 40,
            Fatal = 50
        }
    }
}
